package Events.random;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import structures.Passenger;
import structures.Player;
import structures.Spaceship;
import structures.Status;

public class AlienEncounter implements RandomEvent {

	@Override
	public void execute(Spaceship ship, JTextArea txt, Component parent) {
		Random rnd = new MockRandom();
		String status = "\nYou've encountered some aliens!\n";
		boolean hostile = rnd.nextBoolean();
		int answer = JOptionPane.NO_OPTION;//JOptionPane.showConfirmDialog(parent, "Aliens! Do you want to flee?", "Flee?", JOptionPane.YES_NO_OPTION);
		if(answer == JOptionPane.YES_OPTION){
			status += "\nYou ran away!";
		}
		else if(hostile){
			List<Passenger> allies = new ArrayList<Passenger>();
			for(Passenger p: ship.getPlist()){
				if(p.getStatus() != Status.DEAD && p.getStatus() != Status.CRYOSTASIS)
					allies.add(p);
			}
			int score = ((Player)(ship.getPlist().get(0))).getProfession().killPeople(
					allies.size(), rnd.nextInt(10) + 1, ship.getInventory().getQuantity(6));
			int dcount = 0;
			while(score > allies.size()){
				score -= 1;
				dcount++;
				int ind = rnd.nextInt(allies.size());
				allies.get(ind).setStatus(Status.DEAD);
				status += allies.get(ind).getName() + " is dead.\n";
			}
			score -= dcount;
			while(score > 0){
				score -= 1;
				int ind = rnd.nextInt(allies.size());
				allies.get(ind).setStatus(Status.ISDUMB);
				status += allies.get(ind).getName() + " is dumb now.\n";
			}
			int loot = rnd.nextInt(1000) + 500;
			ship.getInventory().modifyWallet(loot);
			status += "\nYou found " + loot + " imperial credits on the aliens!";
		}
		else{
			int loot = rnd.nextInt(300) - 50;
			ship.getInventory().modifyWallet(loot);
			if(loot < 0)
				status += "\nSome aliens forcibly teleport " + -1 * loot + "imperial credits from your wallet.";
			else
				status += "\nSome aliens forcibly teleport " + loot + "imperial credits into your wallet.";
		}
		txt.append(status);
		txt.append("\n");
	}

}
